package com.sghj.SysPermission.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sghj.SysPermission.entity.SysMenu;
import com.sghj.SysPermission.service.SysMenuService;
import com.sghj.config.ReturnResult;
import com.sghj.config.handler.ResultCode;
import com.sghj.config.handler.exceptionHandler.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xrx
 * @since 2022-02-18
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService menuService;

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @PostMapping("addMenu")
    public ReturnResult addMenu(@RequestBody SysMenu menu){
        if (menu==null){
            throw new MyException(ResultCode.PARAMETER,"缺少参数");
        }
        if (menuService.count(new QueryWrapper<SysMenu>().eq("MenuName",menu.getMenuName()))>0){
            throw new MyException(ResultCode.REPECT,"菜单名请勿重复");
        }
        boolean save = menuService.save(menu);
        if (save){
            return ReturnResult.success().message("添加菜单成功");
        }
        return ReturnResult.failed().message("添加菜单失败");
    }

    /**
     * 获取菜单树-后台
     * @return
     */
    @GetMapping("getMenuTree")
    public ReturnResult getMenuTree(){
        List<SysMenu> list = menuService.getMenuTree();
        if (list!=null){
            return ReturnResult.success().data("menu",list);
        }
        return ReturnResult.failed().message("查询失败");
    }
    /**
     * 获取菜单树-前台
     * @return
     */
    @GetMapping("getMenuTreeFront")
    public ReturnResult getMenuTreeFront(){
        List<SysMenu> list = menuService.getMenuTreeFront();
        if (list!=null){
            return ReturnResult.success().data("menu",list);
        }
        return ReturnResult.failed().message("查询失败");
    }
}

